package homework.week11;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class CheckPwd {

    public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        CheckPwd checkPwd = new CheckPwd();
        System.out.printf("加密后的密文密码:%s%n", checkPwd.hashPW("admin", salt));
        checkPwd.login("", "admin");
        checkPwd.login("admin", "");
        checkPwd.login("admin", "admin");
    }

    // 加密算法
    private final static String ALGORITHOM = "SHA-512";

    // 盐
    private final static String salt = "geekPW";

    // 用户信息列表
    private final List<CustomerInfo> cstList = new ArrayList<>();

    {
        // mock 数据库访问返回数据
        CustomerInfo admin = new CustomerInfo();
        admin.setId("admin");// admin
        admin.setPwd("14d9e755b07"); // admin
        cstList.add(admin);
    }

    // 登录方法
    public void login(String userId, String pwd) {

        if (userId == null || userId.equals("")) {
            System.out.println("用户ID不能为空");
            return;
        }

        if (pwd == null || pwd.equals("")) {
            System.out.println("密码不能为空");
            return;
        }

        CustomerInfo cstInfo = findCstInfoById(userId);
        if (null == cstInfo) {
            throw new RuntimeException("未发现当前用户注册信息");
        }
        try {
            if (checkPW(pwd, cstInfo.getPwd())) {
                System.out.println("登录成功");
            } else {
                System.out.println("用户名或密码错误");
            }
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
            System.out.println("密码验证失败");
            e.printStackTrace();
        }
    }

    /**
     * @param pwd       原始密码
     * @param cipherTxt 密文密码-从数据库获取
     * @return 是否一致
     */
    public boolean checkPW(String pwd, String cipherTxt) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return cipherTxt.equals(hashPW(pwd, salt));
    }

    /**
     * 单项散列加密
     *
     * @param pwd  原始明文密码
     * @param slat 盐
     * @return 密文密码
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    private String hashPW(String pwd, String slat) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest instance = MessageDigest.getInstance(ALGORITHOM);
        instance.update(pwd.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        String newPwd = pwd + slat;
        byte[] data = newPwd.getBytes(StandardCharsets.UTF_8);
        for (byte datum : data) {
            sb.append(Integer.toString(datum & 0xFF + 0x100, 16).substring(1));
        }
        return sb.toString();
    }


    class CustomerInfo {

        private String id;

        private String pwd;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getPwd() {
            return pwd;
        }

        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
    }


    public CustomerInfo findCstInfoById(String userId) {
        List<CustomerInfo> cstList = this.cstList;
        for (CustomerInfo customerInfo : cstList) {
            if (customerInfo.getId().equals(userId)) {
                return customerInfo;
            }
        }
        return null;
    }
}
